import operate from '../common/operate.js'
import store from '../store/index.js'

export default function request(param) {
        // 请求参数
        var url = param.url,
            header = {},
            data = param.data || {}

        //拼接完整请求地址
        var requestUrl = operate.api + url;

        header = {
            'content-type': "application/json",
            'Authorization': uni.getStorageSync('token') || store.getters.token
        };

        // 返回promise
        return new Promise((resolve, reject) => {
            // 请求
            uni.request({
                url: requestUrl,
                data: data,
                method: "POST",
                header: header,
                success: (res) => {
                    // 判断 请求api 格式是否正确
                    if (res.statusCode && res.statusCode != 200) {
                        uni.showToast({
                            title: res.data.message,
                            icon: 'none'
                        });
                        return;
                    }
                    // 将结果抛出
                    resolve(res.data)
                },
                //请求失败
                fail: (e) => {
                    uni.showToast({
                        title: "" + e.data.msg,
                        icon: 'none'
                    });
                    resolve(e.data);
                },
                //请求完成
                complete() {
                    resolve();
                    return;
                }
            })
        })
}